package com.ll.generic;

/**
 * Created with IntelliJ IDEA.
 * User: linlin
 * Date: 13-6-23
 * Time: 下午7:39
 * To change this template use File | Settings | File Templates.
 */
public class Lhist<V> {
    private V[] array;
    private int size;

    public Lhist(int capacity){
//        array = new V[capacity];
        array = (V[]) new Object[capacity];     //很古怪
    }

    public void add(V value){
        if (size == array.length)
            throw new IndexOutOfBoundsException(Integer.toString(size));
        else if (value == null)
            throw new NullPointerException();
        array[size++] = value;
    }

    public void  remove(V value){
        int removalCount = 0;
        for (int i=0; i<size; i++){
          if (array[i].equals(value))
              ++removalCount;
          else if (removalCount > 0){
              array[i-removalCount] = array[i];
              array[i] = null;
          }
        }
        size -= removalCount;
    }

    public int size(){
        return size;
    }

    public V get(int i){
        if(i >=size)
            throw new IndexOutOfBoundsException(Integer.toString(i));
        return array[i];
    }

}
